package com.lifang.imgsoa.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.OSSException;
import com.aliyun.openservices.oss.model.ObjectMetadata;

/**
 * @author Administrator 原图 阿里云OSS工具类
 */
public class OSSObjectUtilOriginal extends OSSObjectUtilBase {

	private static String accessKeyID = Global.ACCESSKEYID_ORIGINAL;
	private static String accessKeySecret = Global.ACCESSKEYSECRET_ORIGINAL;
	private static String bucketName = Global.BUCKETNAME_ORIGINAL;
	
	private static OSSObjectUtilOriginal ossObjectUtilOriginal = new OSSObjectUtilOriginal(accessKeyID, accessKeySecret, bucketName);

	private OSSObjectUtilOriginal(String accessKeyID, String accessKeySecret,String bucketName) {
		super(accessKeyID, accessKeySecret, bucketName);
	}

	public static OSSObjectUtilOriginal getInstance() {
		return ossObjectUtilOriginal;
	}

	public void uploadFile(OSSClient client, String bucketName, String key, String filename) throws OSSException, ClientException, IOException {
        File file = new File(filename);

        ObjectMetadata objectMeta = new ObjectMetadata();
		
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
		objectMeta.setContentLength(file.length());


        InputStream input = new FileInputStream(file);
        client.putObject(bucketName, key, input, objectMeta);
        input.close();
	}
	
	public void uploadFile_stream(OSSClient client, String bucketName, String key, byte[] filebytes) throws OSSException, ClientException, IOException {

        ObjectMetadata objectMeta = new ObjectMetadata();
		
        // 可以在metadata中标记文件类型
        objectMeta.setContentType("image/jpeg");
		objectMeta.setContentLength(filebytes.length);


        InputStream input = new ByteArrayInputStream(filebytes);
        client.putObject(bucketName, key, input, objectMeta);
        input.close();
	}
}